package com.zdb.compont;

import cn.hutool.core.util.ObjectUtil;

import com.zdb.common.Constant;
import com.zdb.common.Result;
import com.zdb.util.JwtUtil;
import com.zdb.util.RenderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
    @Autowired
    private JwtUtil jwtUtil;
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String header = request.getHeader(Constant.REQUEST_HEADER);
        String token = header.replaceAll(Constant.Bearer, Constant.NULL);
        if (ObjectUtil.isNotEmpty(jwtUtil.getLoginUser( token))){
            jwtUtil.delToken(token);
        }
        RenderUtil.renderString(response, Constant.SAFE_CODE, Result.sucess("注销成功"));
    }
}
